package net.zoneland.knowledge.dao;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import net.zoneland.knowledge.model.MeetingSignIn;

/**
 * 会议签到数据接口.
 * @author chw.
 * @version V1.0
 * 2022-11-17
 */
@Mapper
public interface MeetingSignInMapper {

    /**
     * 根据用户id和会议id查询该用户签到情况.
     * @param meetingId 会议id
     * @param signInUserId 用户id
     * @return 签到情况
     */
    @Select({
        "SELECT ID, MEETING_TITLE, MEETING_ID, SIGN_IN_USER_ID, SIGN_IN_USER_NAME, SIGN_IN_DATE, CREATE_TIME, LAST_MODIFIED ",
        "FROM (",
        "SELECT ID, MEETING_TITLE, MEETING_ID, SIGN_IN_USER_ID, SIGN_IN_USER_NAME, SIGN_IN_DATE, CREATE_TIME, LAST_MODIFIED ",
        "FROM AITK_MEETING_SIGN_IN ",
        "WHERE MEETING_ID = #{meetingId, jdbcType = VARCHAR} AND SIGN_IN_USER_ID = #{signInUserId, jdbcType = VARCHAR} ",
        "ORDER BY SIGN_IN_DATE ) ",
        "WHERE ROWNUM = 1"
    })
    @Results(id = "meetingSignIn", value = {
        @Result(column="ID", property="id", jdbcType= JdbcType.VARCHAR, id=true),
        @Result(column="MEETING_TITLE", property="meetingTitle", jdbcType=JdbcType.VARCHAR),
        @Result(column="MEETING_ID", property="meetingId", jdbcType=JdbcType.VARCHAR),
        @Result(column="SIGN_IN_USER_ID", property="signInUserId", jdbcType=JdbcType.VARCHAR),
        @Result(column="SIGN_IN_USER_NAME", property="signInUserName", jdbcType=JdbcType.VARCHAR),
        @Result(column="SIGN_IN_DATE", property="signInDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="CREATE_TIME", property="crateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="LAST_MODIFIED", property="lastModified", jdbcType=JdbcType.TIMESTAMP),
    })
    MeetingSignIn queryIndividualSignIn(@Param("meetingId") final String meetingId,
                                        @Param("signInUserId") final String signInUserId);

    /**
     * 保存用户会议签到信息.
     * @param meetingSignIn 会议签到信息.
     * @return 操作结果
     */
    @Insert({
        "INSERT INTO AITK_MEETING_SIGN_IN ",
        "(ID, MEETING_TITLE, MEETING_ID, SIGN_IN_USER_ID, SIGN_IN_USER_NAME, SIGN_IN_DATE, CREATE_TIME, LAST_MODIFIED) ",
        "VALUES(#{id, jdbcType = VARCHAR}, #{meetingTitle, jdbcType = VARCHAR}, #{meetingId, jdbcType = VARCHAR}, ",
        "#{signInUserId, jdbcType = VARCHAR}, #{signInUserName, jdbcType = VARCHAR}, #{signInDate, jdbcType = TIMESTAMP}, ",
        "#{crateTime, jdbcType = TIMESTAMP}, #{lastModified, jdbcType = TIMESTAMP}) "
    })
    int insert(final MeetingSignIn meetingSignIn);

    /**
     * 更新签到时间.
     * @param meetingId 会议id
     * @param signInUserId 签到人员id
     * @param now 当前时间
     * @return 更新条数
     */
    @Update({
            "update AITK_MEETING_SIGN_IN",
            "set SIGN_IN_DATE = #{now, jdbcType = TIMESTAMP},",
            "LAST_MODIFIED = #{now, jdbcType = TIMESTAMP}",
            "WHERE MEETING_ID = #{meetingId, jdbcType = VARCHAR} AND SIGN_IN_USER_ID = #{signInUserId, jdbcType = VARCHAR} "
    })
    int updateSignInDate(@Param("meetingId") final String meetingId,
                         @Param("signInUserId") final String signInUserId,
                         @Param("now") final LocalDateTime now);

    /**
     * 根据oaId查询我参加的会议
     * @param signInUserId 用户oaId.
     * @return 会议签到集合
     */
    @Select({"SELECT MEETING_ID FROM AITK_MEETING_SIGN_IN WHERE SIGN_IN_USER_ID =  #{signInUserId, jdbcType = VARCHAR}"})
    @Results({
            @Result(column="MEETING_ID", property="meetingId", jdbcType=JdbcType.VARCHAR),
            @Result(column="SIGN_IN_USER_ID", property="signInUserId", jdbcType=JdbcType.VARCHAR),
    })
    List<MeetingSignIn> selectDataBySignInUserId(final @Param("signInUserId") String signInUserId);

    /**
     * 根据id删除数据
     * @param oaIds 人员oaid
     */
    @Delete({
            "<script>",
            "delete FROM AITK_MEETING_SIGN_IN ",
            "WHERE MEETING_ID = #{meetingId, jdbcType = VARCHAR} and SIGN_IN_USER_ID in ",
            "<foreach collection='oaIds' index='index' item='item' open='(' separator=',' close=')'>",
            "#{item} </foreach> ",
            "</script>"
    })
    void deleteByIds(@Param("oaIds") final List<String> oaIds, @Param("meetingId") final String meetingId);

    /**
     * 根据id删除数据
     * @param id
     */
    @Delete({"delete FROM AITK_MEETING_SIGN_IN WHERE id = #{id, jdbcType = VARCHAR}"})
    void deleteById(final @Param("id") String id);


    @Insert({"INSERT INTO AITK_MEETING_SIGN_IN (ID, MEETING_TITLE, MEETING_ID, MOBILE, JOB, SIGN_IN_USER_ID, SIGN_IN_USER_NAME, DEPT_NAME, EMP_NO, COMPANY_NAME, COMPANY_DN) " +
            "VALUES (#{id, jdbcType = VARCHAR}, #{meetingTitle, jdbcType = VARCHAR}, #{meetingId, jdbcType = VARCHAR}, #{mobile, jdbcType = VARCHAR}, #{job, jdbcType = VARCHAR}, " +
            "#{signInUserId, jdbcType = VARCHAR}, #{signInUserName, jdbcType = VARCHAR}, #{deptName, jdbcType = VARCHAR}," +
            "#{empNo, jdbcType = VARCHAR}, #{companyName, jdbcType = VARCHAR}, #{companyDn, jdbcType = VARCHAR} )"})
    void save(final MeetingSignIn meetingSignIn);

    /**
     * 根据会议id查询参会人员数据
     * @param meetingId 会议id
     * @return
     */

    @Select({"SELECT ID, MEETING_TITLE, MEETING_ID, DEPT_NAME, COMPANY_NAME, SIGN_IN_USER_NAME, SIGN_IN_USER_ID, SIGN_IN_DATE, JOB, MOBILE, DEPT_NAME FROM AITK_MEETING_SIGN_IN WHERE MEETING_ID = #{meetingId, jdbcType = VARCHAR}"})
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MEETING_TITLE", property = "meetingTitle", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MEETING_ID", property = "meetingId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_NAME", property = "deptName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "COMPANY_NAME", property = "companyName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SIGN_IN_USER_NAME", property = "signInUserName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SIGN_IN_USER_ID", property = "signInUserId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SIGN_IN_DATE", property = "signInDate", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "JOB", property = "job", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MOBILE", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_NAME", property = "deptName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MEETING_ID", property = "meetingId", jdbcType = JdbcType.VARCHAR),
    })
    List<MeetingSignIn> querySignInUserByMeetingId(final String meetingId);

    /**
     * 批量删除签到人员
     * @param ids 签到人员id
     */
    @Delete({
            "<script>",
            "DELETE FROM AITK_MEETING_SIGN_IN WHERE ID IN",
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int batchDeleteById(final @Param("ids") List<String> ids);


    /**
     * 保存参会人员数据
     * @param meetingSignIn
     */
    @Insert({
            "INSERT INTO AITK_MEETING_SIGN_IN ",
            "(ID, MEETING_TITLE, MEETING_ID, SIGN_IN_USER_ID, SIGN_IN_USER_NAME, SIGN_IN_DATE, CREATE_TIME, LAST_MODIFIED, MOBILE, EMP_NO, JOB, COMPANY_DN, COMPANY_NAME, ORDER_NUM, DEPT_NAME) ",
            "VALUES(#{id, jdbcType = VARCHAR}, #{meetingTitle, jdbcType = VARCHAR}, #{meetingId, jdbcType = VARCHAR}, ",
            "#{signInUserId, jdbcType = VARCHAR}, #{signInUserName, jdbcType = VARCHAR}, #{signInDate, jdbcType = TIMESTAMP}, ",
            "#{crateTime, jdbcType = TIMESTAMP}, #{lastModified, jdbcType = TIMESTAMP}, #{mobile, jdbcType = VARCHAR}, #{empNo, jdbcType = VARCHAR}, #{job, jdbcType = VARCHAR},",
            "#{companyDn, jdbcType = VARCHAR}, #{companyName, jdbcType = VARCHAR}, #{orderNum, jdbcType = INTEGER}, #{deptName, jdbcType = VARCHAR})"
    })
    void insertMeetingSignInUserData(final MeetingSignIn meetingSignIn);
}
